package com.leyou.item.web;


import com.github.pagehelper.PageInfo;
import com.leyou.item.pojo.Manager;
import com.leyou.item.service.ManagerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:yanjizhw@163.com
 * @date:2019-10-10 16:27
 * 描述:
 * @since:1.0.0
 */
@Controller
@RequestMapping("manager")
public class ManagerController {

    @Autowired
    private ManagerService managerService;

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String login(RedirectAttributes  rModel, Model model, HttpServletRequest request, Manager manager){

        Manager user =  managerService.login(manager);

        if (StringUtils.isBlank(manager.getUsername())){
            model.addAttribute("error", "用户名不可以为空");
            return "/login";
        }else if (StringUtils.isBlank(manager.getPassword())){
            model.addAttribute("error", "密码不可以为空");
            return "/login";
        }else if (user == null){
            model.addAttribute("error", "查询不到指定的用户");
            return "/login";
        }else if (!user.getPassword().equals(manager.getPassword())){
            model.addAttribute("error", "用户名或密码错误");
            return "/login";
        }else if (user.getStatus() == 0){
            model.addAttribute("error", "用户已被冻结，请联系管理员");
            return "/login";
        }else if (user.getStatus() == 2){
            model.addAttribute("error", "用户已被删除，请联系管理员");
            return "/login";
        }else {
            rModel.addFlashAttribute("username", user.getUsername());
            request.getSession().setAttribute("user", user);
            return "redirect:/index.page";
        }
    }

    @RequestMapping("/signOut")
    public String signOut(HttpServletRequest request){

        request.getSession().removeAttribute("user");

        return "redirect:/login.page";
    }

    @ResponseBody
    @RequestMapping("list")
    public ResponseEntity<Map<String, Object>> queryManager(@RequestParam(value="page", defaultValue = "1") Integer page){


        Map<String, Object> map = new HashMap<>();

        int pageSize = 12; //每页显示条数

        PageInfo<Manager> info = managerService.queryManager(page, pageSize);


        long totalPage = info.getTotal() / pageSize;

        if (info.getTotal() % pageSize != 0){
            totalPage ++;
        }

        // 封装数据，并返回
        map.put("page", page);
        map.put("pageSize", pageSize);
        map.put("totalPage", totalPage);
        map.put("list", info.getList());

        return ResponseEntity.ok(map);
    }
}
